class Solution:
    def convert(self, s: str, numRows: int) -> str:
        # 对于第n行我们可以有 这里n从0开始
        # 每个周期的第一个位置为n + (rowNum-1)*2  第二个位置为 2*(rowNum-1)-n+(rowNum-1)*2
        # 那么对于每个周期i 这里i从0开始
        # 每个周期的第一个位置为n + (rowNum-1)*2*i  第二个位置为 2*(rowNum-1)-n+(rowNum-1)*2*i
        str_length = len(s)
        x = str_length / ((numRows - 1) * 2) if (numRows - 1) * 2 != 0 else 0
        i_max = int(x) if x == int(x) or x < 0 else int(x) + 1

        res: str = ""

        n: int = 0
        if numRows == 1:
            return s
        while n <= numRows - 1:
            i: int = 0
            while i <= i_max:
                # 第一行
                if n == 0:
                    index = n + (numRows - 1) * 2 * i
                    if index < str_length:
                        res += s[index]
                # 中间部分
                elif n != 0 and n != numRows - 1:
                    index_1 = n + (numRows - 1) * 2 * i
                    index_2 = 2 * (numRows - 1) - n + (numRows - 1) * 2 * i
                    if index_1 < str_length:
                        res += s[index_1]
                    if index_2 < str_length:
                        res += s[index_2]
                elif n == numRows - 1:
                    index = n + (numRows - 1) * 2 * i
                    if index < str_length:
                        res += s[index]
                i += 1
            n += 1
        return res


s = "PAYPALISHIRING"
numRows = 3
solution = Solution()
result = solution.convert(s, numRows)
print(f"Test 1: {result}")
